//
//  DYZSearchCarsController.m
//  发布车源
//
//  Created by xmy on 16/8/18.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "DYZSearchCarsController.h"
#import "UILabel+Addition.h"
#import "Masonry.h"
#import "DYZSearchCarsBaseCell.h"
#import "DYZEffectDateController.h"
#import "DYZCategoriesController.h"
#import "DYZChooseBrandController.h"
#import "DYZChooseOutColorController.h"
#import "DYZCardCityController.h"
#import "DYZPutCarsSourceController.h"
#import "DYZChooseCarModelController.h"

static NSString *normalid = @"normalid";
static NSString *secondid = @"secondid";
static NSString *threeid = @"threeid";
static NSString *sevenid = @"sevenid";
static NSString *eightid = @"eightid";


@interface DYZSearchCarsController ()
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,assign)CGFloat eightHeight;
@property(nonatomic,strong)NSIndexPath *indexPath;

@property(copy,nonatomic)NSString *result;

@property(nonatomic,strong)NSArray *roadData;


@end

@implementation DYZSearchCarsController

- (void)viewDidLoad {
    
    NSLog(@"%@",self.result);
    
    [super viewDidLoad];
    self.navigationItem.title = @"发布车源";
    self.dataArray  = [self loadData];
    
    [self setupUI];
    
    //右侧保存按钮
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(saveClick)];
    self.navigationItem.rightBarButtonItem = item;
    
    //注册车型通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getInfo:) name:@"hello" object:nil];

    //注册自定义颜色
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getInfo:) name:@"random" object:nil];
    
    //注册颜色
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getInfo:) name:@"color" object:nil];
    
}
//车型通知
- (void)getInfo:(NSNotification *)sender{
    self.result = sender.userInfo[@"title"];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    //刷新某一行数据
    [self.tableView reloadRowsAtIndexPaths:self.roadData withRowAnimation:UITableViewRowAnimationNone];
    
    
}

//保存按钮事件
- (void)saveClick{
    
    //跳转到推荐车源
    DYZPutCarsSourceController *put = [[DYZPutCarsSourceController alloc]init];
    [self.navigationController pushViewController:put animated:YES];
    
}

- (void)setupUI{
    //注册
    [self.tableView registerNib:[UINib nibWithNibName:@"DYZSearchCarsNormal" bundle:nil] forCellReuseIdentifier:normalid];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYZSearchCarSecond" bundle:nil] forCellReuseIdentifier:secondid];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYZSearchCarsThree" bundle:nil] forCellReuseIdentifier:threeid];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYZSearchCarsSeven" bundle:nil] forCellReuseIdentifier:sevenid];
    // 加载xib
    UINib* nib = [UINib nibWithNibName:@"DYZSearchCarsEight" bundle:nil];
    // 获取xib中的视图
    UIView* pictureView = [nib instantiateWithOwner:nil options:nil][0];
    
    CGFloat xibH = pictureView.bounds.size.height;
    self.eightHeight = xibH;
    
    [self.tableView registerNib:nib forCellReuseIdentifier:eightid];
    
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
}

- (NSArray *)loadData{
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"searchCars.plist" ofType:nil];
    
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    
    return array;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    [tableView rel];
    NSString *reuseId;
    
    if(indexPath.row == 2)
    {
        reuseId = secondid;
        
    }else if(indexPath.row == 3)
    {
        reuseId =threeid;
    }else if(indexPath.row == 7)
    {
        reuseId = sevenid;
    }else if(indexPath.row == 8)
    {
        reuseId = eightid;
    }else{
        reuseId = normalid;
    }
    
    DYZSearchCarsBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId forIndexPath:indexPath];
    
    cell.title.text = self.dataArray[indexPath.row];
    cell.text.text = self.result;
    
    return cell;
}


// cell的点击事件
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    //      取消cell的选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    NSIndexPath *indexPath_1=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
//    NSLog(@"%ld",(long)indexPath.row);
    self.roadData =[NSArray arrayWithObject:indexPath_1];
   
//    [tableView reloadData];
    if(indexPath.row == 6)
    {
        DYZEffectDateController *effect = [[DYZEffectDateController alloc]init];
        
        [self.navigationController pushViewController:effect animated:YES];
        
    }else if(indexPath.row == 0)
    {
        DYZCategoriesController *Categroy = [[DYZCategoriesController alloc]init];
        Categroy.dataBlock = ^(NSString *data){
            self.result = data;
    };
        
        [self.navigationController pushViewController:Categroy animated:YES];
    }else if(indexPath.row == 1)
    {
        DYZChooseBrandController *vc = [[DYZChooseBrandController alloc]init];
        
//        DYZChooseCarModelController *vc1 = [[DYZChooseCarModelController alloc]init];
        //数据传递
//        vc1.dataBlock = ^(NSString *data){
//            
//            self.result = data;
//            NSLog(@"%@",data);
//        };

//        self.result = vc.result;
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if(indexPath.row == 4)
    {
        DYZChooseOutColorController *outColor = [[DYZChooseOutColorController alloc]init];
        
        [self.navigationController pushViewController:outColor animated:YES];
    }else if(indexPath.row == 5)
    {
        DYZCardCityController *cardCity = [[DYZCardCityController alloc]init];
        
        [self.navigationController pushViewController:cardCity animated:YES];
    }


    
    //数据回传
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;{
    if(indexPath.row == 8)
    {
        return self.eightHeight;
        
    }else{
        return 45;
    }
    return 0;
}
//移除通知
-(void)dealloc {
    //用完之后一定要记得删除
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
